package com.jframe.sys.utils;

import org.apache.jcs.JCS;

public class CacheUtil {
  
	private static CacheUtil instance;
	private static int checkedOut = 0;
	public static JCS ObjCache;

	public CacheUtil(String cachespace){
		init(cachespace);
	}
	
	private void init(String cachespace) {
		try {
			ObjCache = JCS.getInstance(cachespace);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static CacheUtil getInstance(String cachespace) {
		synchronized (CacheUtil.class) {
			if (instance == null) {
				instance = new CacheUtil(cachespace);
			}
		}
		synchronized (instance) {
			instance.checkedOut++;
		}
		return instance;
	}

	public Object get(Object key) {
		Object obj = null;
		obj = (Object) ObjCache.get(key);
		return obj;
	}

	public void put(Object key, Object obj) {
		try {
			if (!key.equals("")) {
				ObjCache.remove(key);
			}
			ObjCache.put(key, obj);
		} catch (Exception e) {
		}
	}

	public void clear(Object key) {
		try {
			ObjCache.remove(key);
		} catch (Exception e) {
		}
	}

	public void clear() {
		try {
			ObjCache.clear();
		} catch (Exception e) {
		}
	}

	public static void main(String[] args) {
		//CacheUtil cache = CacheUtil.getInstance("datacache");
		//System.out.println("test1 is  " +cache.get("sm_sys_menu"));
		//System.out.println("test2 is  " + cache.get("sm_sys_menu_tree"));
		//System.out.println("test3 is  " + dto.getObj("test3"));
		//System.out.println("test4 is  " + dto.getObj("test4"));*/
	}

}
